package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.exception.BusinessException;
import com.apache.ius.common.CommonUtils;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化账户（渠道商）资金余额对象
 */
public class InitUserBalancePluginImpl implements CustomMethodPlugin {
    private Logger log = Logger.getLogger(getClass());
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String userId = String.valueOf(map.get("userId"));//渠道商ID
        if(StrUtil.isNull(userId)){
            throw new BusinessException("渠道商ID不能为空");
        }
        Map<String,Object> wMap2 = new HashMap<>();
        wMap2.put("uctUser.w_userId", userId);
        ResultEntity resultEntity2 = IusPluginUtil.getInstance().select("plateform", "obj", "s_uctUser", wMap2);
        if(resultEntity2!=null && resultEntity2.getEntity()!=null){
            DataMap userObj = (DataMap)resultEntity2.getEntity();
            String userEname = String.valueOf(userObj.get("userEname"));
            String userCname = String.valueOf(userObj.get("userCname"));//渠道商名称
            String userOwner = StrUtil.doNull(String.valueOf(userObj.get("userTitle")),"");//开户人姓名

            Map<String,Object> wMap1 = new HashMap<>();
            wMap1.put("ssUserBalance.w_userId", userId);
            ResultEntity resultEntity1 = IusPluginUtil.getInstance().select("obj", "s_ssUserBalance", wMap1);
            if(resultEntity1!=null && resultEntity1.getEntity()!=null){
                log.info("账户["+userId+"-"+userCname+"]资金余额对象已存在，无需初始化！");
                DataMap userBalanceMap = (DataMap) resultEntity1.getEntity();
                String userBalance_userOwner =String.valueOf( userBalanceMap.get("userOwner"));//开户人姓名
                String userBalance_userCname =String.valueOf( userBalanceMap.get("userCname"));//渠道商名称
                Map<String,Object> pMap = new HashMap<>();
                if(!StrUtil.isNull(userCname) && !userCname.equals(userBalance_userCname)){
                    log.info("账户同步更新字段[userCname]["+userBalance_userCname+"]为["+userCname+"]");
                    pMap.put("ssUserBalance.userCname", userCname);
                }
                if(!StrUtil.isNull(userOwner) && !userOwner.equals(userBalance_userOwner)){
                    log.info("账户同步更新字段[userOwner]["+userBalance_userOwner+"]为["+userOwner+"]");
                    pMap.put("ssUserBalance.userOwner", userOwner);
                }
                if(pMap.size()>0){
                    pMap.put("ssUserBalance.w_userId", userId);
                    ResultEntity resultEntity4 = IusPluginUtil.getInstance().saveInvoke(CommonUtils.getSysEname(),"u_ssUserBalance",pMap);
                    log.info("账户同步更名完成->"+ JSONObject.fromObject(resultEntity4));
                }
            }else{
                Map<String,Object> pMap = new HashMap<>();
                pMap.put("ssUserBalance.createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                pMap.put("ssUserBalance.userId", userId);
                pMap.put("ssUserBalance.userEname", userEname);
                pMap.put("ssUserBalance.userCname", userCname);
                pMap.put("ssUserBalance.userOwner", userOwner);
                pMap.put("ssUserBalance.freezeBalance", 0);
                pMap.put("ssUserBalance.totalCashOut", 0);
                pMap.put("ssUserBalance.availableBalance", 0);
                ResultEntity resultEntity3 = IusPluginUtil.getInstance().saveInvoke(CommonUtils.getSysEname(),"i_ssUserBalance",pMap);
                log.info("初始化账户["+userId+"-"+userCname+"]资金余额对象->"+ JSONObject.fromObject(resultEntity3));
            }
        }else{
            log.info("账户["+userId+"]对象不存在");
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put(CustomMethodPlugin.resultMark, "同步操作账户余额信息成功");
        return result;
    }


    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }
}
